using System;
using System.Collections;
using System.Data;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarUsageDB
    {
        public CarUsageDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        

        public Int64 Insert(CarUsage carusage)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarUsageInsertUpdateSingleItem]";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@ProviderCarUsageId", carusage.ProviderCarUsageId);
            _sPara.Add("@ProviderCompanyId", carusage.ProviderCompanyId);
            _sPara.Add("@CXCarUsageId", carusage.CXCarUsageId);
            _sPara.Add("@CarUsageDesc", carusage.CarUsageDesc);
            _sPara.Add("@UsageRate", carusage.UsageRate);
            _sPara.Add("@RefCEUserId", carusage.RefCEUserId);
            _sPara.Add("@Status", carusage.Status);
            _sPara.Add("@Finfo1", carusage.FInfo1);
            _sPara.Add("@Dinfo1", carusage.DInfo1);
            #endregion
     
            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 ProviderCarUsageId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarUsageDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarUsageId", ProviderCarUsageId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;

        }

        public CarUsageList GetProviderCarUsageList(Int64 ProviderCompanyId)
        {
            CarUsageList _carusagelist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarUsageSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carusagelist = new CarUsageList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarUsage _carusage = new CarUsage();
                        _carusage = FillDataRecord(_dt, i);
                        _carusagelist.Add(_carusage);
                    }
                }
            }
            return _carusagelist;
        }


        public CarUsage GetProviderCarUsage(Int64 ProviderCompanyId, Int32 CXCarUsageId)
        {
            CarUsage _carusage = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarUsageSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXCarUsageId", CXCarUsageId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carusage = new CarUsage();
                    _carusage = FillDataRecord(_dt, 0);
                }
            }
            return _carusage;
        }

        public CarUsage GetProviderCarUsage(Int64 ProviderCarUsageId)
        {
            CarUsage _carusage = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarUsageSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarUsageId", ProviderCarUsageId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carusage = new CarUsage();
                    _carusage = FillDataRecord(_dt, 0);
                }
            }
            return _carusage;
        }
        
         public DataTable GetCXCarUsage()
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Result");
            string _spName = "sprocCarUsageSelectItemList";
            SortedList _sPara = new SortedList();

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        private CarUsage FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarUsage myCarUsage = new CarUsage();
            myCarUsage.ProviderCarUsageId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarUsageId"].ToString());
            myCarUsage.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarUsage.CarUsageDesc = myDataTable.Rows[RowId]["CarUsageDesc"].ToString();
            myCarUsage.CXCarUsageId = Convert.ToInt32(myDataTable.Rows[RowId]["CXCarUsageId"].ToString());
            myCarUsage.UsageRate = Convert.ToSingle(myDataTable.Rows[RowId]["UsageRate"].ToString());
            myCarUsage.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarUsage.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarUsage.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarUsage.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarUsage.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myCarUsage;

 
        }
        
    }
}